﻿using BLL;
using DAL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Model;
using Microsoft.AspNetCore.StaticFiles;

namespace HotelCode.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class WilltellController : ControllerBase
    {
        public WiliteBLL WiliteBLL { get; }

        public WilltellController(WiliteBLL wiliteBLL)
        {
            WiliteBLL = wiliteBLL;
        }
        [HttpPost]
        /// <summary>
        /// 添加会告
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public IActionResult WillteAdd(WilltellModel info)
        {
            try
            {
                int res= WiliteBLL.WillteAdd(info);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpDelete]
         
        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult willtellDelete(int id)
        {
            try
            {
                int res= WiliteBLL.willtellDelete(id);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpDelete]
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult willtellDeleteAll(string ids)
        {
            try
            {
               int res=WiliteBLL.willtellDeleteAll(ids);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpPut]
        /// <summary>
        /// 批量修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public IActionResult willtellUpdateAll(string ids, int state)
        {
            try
            {
                var res=WiliteBLL.willtellUpdateAll(ids, state);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        /// <summary>
        /// id查询(反填)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult GetwillteId(int id)
        {
            try
            {
                var list=WiliteBLL.GetwillteId(id);
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpPut]
        /// <summary>
        /// 修改会告信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public IActionResult willteUpdate(WilltellModel info)
        {
            try
            {
               var list=WiliteBLL.willteUpdate(info);
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        /// <summary>
        /// 显示查询分页
        /// </summary>
        /// <param name="totalCount"></param>
        /// <param name="totalPage"></param>
        /// <param name="pageindex"></param>
        /// <param name="pageSize"></param>
        /// <param name="WiteTitle"></param>
        /// <param name="WiteType"></param>
        /// <returns></returns>
        public IActionResult GetWiteList(int pageindex = 1, int pageSize = 2, string? WiteTitle = "", int? WiteType = null)
        {
            try
            {
                 int totalCount = 0;  int totalPage = 0;
                var list=WiliteBLL.GetWiteList(out totalCount, out totalPage, pageindex, pageSize, WiteTitle, WiteType);
                return Ok(new
                {
                    list = list,
                    totalCount = totalCount,
                    totalPage = totalPage,
                });
            }
            catch (Exception)
            {

                throw;
            }
        }
        [HttpGet]
        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pageSize"></param>
        /// <param name="WiteTitle"></param>
        /// <param name="WiteType"></param>
        /// <returns></returns>
        public IActionResult GetImportExSxle(int pageindex = 1, int pageSize = 2, string? WiteTitle = "", int? WiteType = null)
        {
            try
            {
                byte[] data = null;
                string t = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                string f = "会告列表.xlsx";
                IWorkbook b=new XSSFWorkbook();
                ISheet s= b.CreateSheet();
                IRow row1=s.CreateRow(0);
                row1.CreateCell(0).SetCellValue("标题");
                row1.CreateCell(1).SetCellValue("会告内容");
                row1.CreateCell(2).SetCellValue("会告照片");
                row1.CreateCell(3).SetCellValue("会告状态");
                int totalCount = 0; int totalPage = 0;
                var list = WiliteBLL.GetWiteList(out totalCount, out totalPage, pageindex, pageSize, WiteTitle, WiteType);
                int Rowindex = 1;
                foreach (var item in list)
                {
                    IRow row = s.CreateRow(Rowindex);
                    row.CreateCell(0).SetCellValue(item.WiteTitle);
                    row.CreateCell(1).SetCellValue(item.WiteDesc);
                    row.CreateCell(2).SetCellValue(item.Photo);
                    row.CreateCell(3).SetCellValue(item.State);
                    Rowindex++;
                }
                using (MemoryStream ms=new MemoryStream())
                {
                    b.Write(ms);
                    data = ms.ToArray();
                }
                return File(data, t,f);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
